package ru.rchervot.sanasar.client.place;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.inject.Inject;
import com.google.inject.name.Named;

public class ManagerPlacesDefault implements ManagerPlaces {

	private final FactoryPlace factoryPlaceCandidates;
	private final FactoryPlace factoryPlaceSettings;
	
	@Inject
	public ManagerPlacesDefault(@Named("Candidates") FactoryPlace placeFactoryCandidates,
			@Named("Settings") FactoryPlace placeFactorySettings) {
		this.factoryPlaceCandidates = placeFactoryCandidates;
		this.factoryPlaceSettings = placeFactorySettings;
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		HistoryTokenObject hto = HistoryTokenObject.build(event.getValue());
		
		Place place = null;
		
		if (hto.getModuleName().equalsIgnoreCase(Constants.MODULE_CAND)) {
			place = factoryPlaceCandidates.buildPlace(hto);
		} else if (hto.getModuleName().equalsIgnoreCase(Constants.MODULE_SETTINGS)) {
			place = factoryPlaceSettings.buildPlace(hto);
		} else {
			place = factoryPlaceCandidates.getCurrentPlace();
		}
		
		if (place != null) {
			place.go(hto.getPlaceParameters());
		}
		
		System.out.println("place: " + place);
	}

	@Override
	public String getStartHistoryToken() {
		return factoryPlaceCandidates.getCurrentPlace().historyToken();
	}
}
